package org.ccay.core.request.impl;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import org.ccay.core.request.IApplicationPrincipal;
import org.ccay.core.request.IRequestContext;
import org.ccay.core.request.IUserPrincipal;

/**
 * 请求上下文
 * @author chaowangbang
 * @since 2011-8-25
 */
public final class RequestContext implements IRequestContext {

	private static final long serialVersionUID = 5296206775558799330L;
	private String userLanguage;
	private IApplicationPrincipal application;
	private IUserPrincipal user; 
	private Map<String,Object> items = new HashMap<String,Object>();
	private Map<String,Serializable> session = new HashMap<String,Serializable>();
	private boolean isSkipSecurityCheck;	
	private boolean debugEnabled;
	private String sessionId;
	private String userIp;
	
	/**
	 * @return the sessionId
	 */
	public String getSessionId() {
		return sessionId;
	}
	/**
	 * @param sessionId the sessionId to set
	 */
	public void setSessionId(String sessionId) {
		this.sessionId = sessionId;
	}
	/**
	 * @return the debugEnabled
	 */
	public boolean isDebugEnabled() {
		return debugEnabled;
	}
	/**
	 * @param debugEnabled the debugEnabled to set
	 */
	public void setDebugEnabled(boolean debugEnabled) {
		this.debugEnabled = debugEnabled;
	}
	

	/* (non-Javadoc)
	 * @see org.ccay.core.request.IRequestContext#getApplication()
	 */
	public IApplicationPrincipal getApplication() {
		return application;
	}
	/**
	 * @param application the application to set
	 */
	public void setApplication(IApplicationPrincipal application) {
		this.application = application;
	}
	/* (non-Javadoc)
	 * @see org.ccay.core.request.IRequestContext#getItems()
	 */
	public Map<String, Object> getItems() {
		return items;
	}
	/**
	 * @param items the items to set
	 */
	public void setItems(Map<String, Object> items) {
		this.items = items;
	}
	/* (non-Javadoc)
	 * @see org.ccay.core.request.IRequestContext#getUser()
	 */
	public IUserPrincipal getUser() {
		return user;
	}
	/**
	 * @param user the user to set
	 */
	public void setUser(IUserPrincipal user) {
		this.user = user;
	}

	/* (non-Javadoc)
	 * @see org.ccay.core.request.IRequestContext#getUserIp()
	 */
	public String getUserIp() {
		return userIp;
	}
	/**
	 * @param userIp the userIp to set
	 */
	public void setUserIp(String userIp) {
		this.userIp = userIp;
	}
	/* (non-Javadoc)
	 * @see org.ccay.core.request.IRequestContext#getUserLanguage()
	 */
	public String getUserLanguage() {
		return userLanguage;
	}
	/**
	 * @param userLanguage the userLanguage to set
	 */
	public void setUserLanguage(String userLanguage) {
		this.userLanguage = userLanguage;
	}
	/* (non-Javadoc)
	 * @see org.ccay.core.request.IRequestContext#getItem(java.lang.String)
	 */
	public 	Object getItem(String key) {
		return items.get(key);
	}
	/**
	 * 设置请求上下文中的自定义参数
	 */
	public void setItem(String key, Object value) {
		items.put(key, value);		
	}
	/**
	 * 获取当前的请求上下文，为空时返回异常
	 */
	public static IRequestContext getCurrent(){
		return RequestContextManager.getCurrent(false);
	}
	
	/**
	 * 获取当前的请求上下文	
	 * @author chaowangbang
	 * @since 2012-4-23
	 * @param allowNull 是否允许空的
	 * @return
	 */
	public static IRequestContext getCurrent(boolean allowNull){
		return RequestContextManager.getCurrent(allowNull);
	}
	/**
	 * session对象
	 */
	public Map<String, Serializable> getSession() {
		return session;
	}
	/**
	 * session对象
	 */
	public void setSession(Map<String, Serializable> session) {
		this.session = session;
	}
	
	/**
	 * 获取session对象元素
	 */
	public 	Serializable getSessionItem(String key) {
		return session.get(key);
	}
	/**
	 * 设置session对象元素
	 */
	public void setSessionItem(String key, Serializable value) {
		session.put(key, value);		
	}
	/**
	 * @return the isSkipSecurityCheck
	 */
	public boolean isSkipSecurityCheck() {
		return isSkipSecurityCheck;
	}
	/**
	 * @param isSkipSecurityCheck the isSkipSecurityCheck to set
	 */
	public void setSkipSecurityCheck(boolean isSkipSecurityCheck) {
		this.isSkipSecurityCheck = isSkipSecurityCheck;
	}
}
